%%%% -*- Erlang -*-
-module(c2a).
%-export([c2a/1]).
-compile(export_all).
-include_lib("kernel/include/file.hrl").

enum({function, N, B}) ->
    do_fun({function, N, B});
enum(T) ->
    {R, L} = T,
    do_fun(L),
    enum(R).



gen_funcs(H|T) ->
    %%% TODO generate ant code from following functions, entry point is head...


pp()->
    {move, fun()->123 end, fun()->222end}.

P = {move, fun()->123 end, fun()->222 end}.
{move,#Fun<erl_eval.20.67289768>,#Fun<erl_eval.20.67289768>}
(my_nam@127.0.0.1)482> {What, SuccN, FailN} = P.
{move,#Fun<erl_eval.20.67289768>,#Fun<erl_eval.20.67289768>}
(my_nam@127.0.0.1)483> SuccN().
123
(my_nam@127.0.0.1)484> 




%% @spec do_fun -> fun(Offset) -- position independent state machine instructions .. ;) 
do_fun({function, S, _}) ->
    {string, _, N} = S,
    io:format("Func: ~s.~n", [N]).

tree(P) ->
%%     {ok, I} = file:read_file_info(P),
%%     S = I#file_info.size,
    {ok, B} = file:read_file(P),
    S = binary_to_list(B),
    {_, T, _} = c2a_lexer:string(S),
    T.


c(P) ->
%%     {ok, I} = file:read_file_info(P),
%%     S = I#file_info.size,
    {ok, B} = file:read_file(P),
    S = binary_to_list(B),
    {_, T, _} = c2a_lexer:string(S),
    c2a_parser:parse(T).







%% {function,{string,1,"main"},
%%           {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%              {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                      {pickup,8},
%%                      {flip,9}}},
%%             {branch,{funcall,{string,11,"foo"}},
%%                     {move,12},
%%                     {sense,{right_ahead,13},{food,13}}}},
%%            '$undefined'}}
%% (my_nam@127.0.0.1)450> c2a:enum(WWW).
%% ** exception error: no match of right hand side value 
%%                     {function,
%%                         {string,1,"main"},
%%                         {{{{branch,
%%                                {sense,{ahead,3},{food,3}},
%%                                {pickup,4},
%%                                {flip,5}},
%%                            {branch,
%%                                {sense,{left_ahead,7},{foe_with_food,7}},
%%                                {pickup,8},
%%                                {flip,9}}},
%%                           {branch,
%%                               {funcall,{string,11,"foo"}},
%%                               {move,12},
%%                               {sense,{right_ahead,13},{food,13}}}},
%%                          '$undefined'}}
%%      in function  c2a:enum/1
%% (my_nam@127.0.0.1)451> WWW.
%% {function,{string,1,"main"},
%%           {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%              {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                      {pickup,8},
%%                      {flip,9}}},
%%             {branch,{funcall,{string,11,"foo"}},
%%                     {move,12},
%%                     {sense,{right_ahead,13},{food,13}}}},
%%            '$undefined'}}
%% (my_nam@127.0.0.1)452> c("c2a").
%% ./c2a.erl:10: function enum/1 already defined
%% error
%% (my_nam@127.0.0.1)453> c("c2a").
%% {ok,c2a}
%% (my_nam@127.0.0.1)454> c2a:enum(WWW).
%% ** exception error: no match of right hand side value 
%%                     {function,
%%                         {string,1,"main"},
%%                         {{{{branch,
%%                                {sense,{ahead,3},{food,3}},
%%                                {pickup,4},
%%                                {flip,5}},
%%                            {branch,
%%                                {sense,{left_ahead,7},{foe_with_food,7}},
%%                                {pickup,8},
%%                                {flip,9}}},
%%                           {branch,
%%                               {funcall,{string,11,"foo"}},
%%                               {move,12},
%%                               {sense,{right_ahead,13},{food,13}}}},
%%                          '$undefined'}}
%%      in function  c2a:enum/1
%% (my_nam@127.0.0.1)455> c("c2a").
%% {ok,c2a}
%% (my_nam@127.0.0.1)456> c2a:enum(WWW).
%% Func: main.
%% ok
%% (my_nam@127.0.0.1)457> c2a:enum(TP).
%% Func: bar.
%% Func: foo.
%% Func: main.
%% ok
%% (my_nam@127.0.0.1)458> c2a:c("sample.c").
%% {ok,{{{function,{string,1,"main"},
%%                 {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%                    {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                            {pickup,8},
%%                            {flip,9}}},
%%                   {branch,{funcall,{string,11,"foo"}},
%%                           {move,12},
%%                           {sense,{right_ahead,13},{food,13}}}},
%%                  '$undefined'}},
%%       {function,{string,16,"foo"},
%%                 {{{{move,18},{sense,{ahead,19},{home,19}}},
%%                   {branch,{sense,{right_ahead,21},{foe_home,21}},
%%                           {pickup,22},
%%                           {flip,23}}},
%%                  '$undefined'}}},
%%      {function,{string,27,"bar"},
%%                {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%                 '$undefined'}}}}
%% (my_nam@127.0.0.1)459> f().
%% ok
%% (my_nam@127.0.0.1)460> T = c2a:c("sample.c").
%% {ok,{{{function,{string,1,"main"},
%%                 {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%                    {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                            {pickup,8},
%%                            {flip,9}}},
%%                   {branch,{funcall,{string,11,"foo"}},
%%                           {move,12},
%%                           {sense,{right_ahead,13},{food,13}}}},
%%                  '$undefined'}},
%%       {function,{string,16,"foo"},
%%                 {{{{move,18},{sense,{ahead,19},{home,19}}},
%%                   {branch,{sense,{right_ahead,21},{foe_home,21}},
%%                           {pickup,22},
%%                           {flip,23}}},
%%                  '$undefined'}}},
%%      {function,{string,27,"bar"},
%%                {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%                 '$undefined'}}}}
%% (my_nam@127.0.0.1)461> {H, L} = T.
%% {ok,{{{function,{string,1,"main"},
%%                 {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%                    {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                            {pickup,8},
%%                            {flip,9}}},
%%                   {branch,{funcall,{string,11,"foo"}},
%%                           {move,12},
%%                           {sense,{right_ahead,13},{food,13}}}},
%%                  '$undefined'}},
%%       {function,{string,16,"foo"},
%%                 {{{{move,18},{sense,{ahead,19},{home,19}}},
%%                   {branch,{sense,{right_ahead,21},{foe_home,21}},
%%                           {pickup,22},
%%                           {flip,23}}},
%%                  '$undefined'}}},
%%      {function,{string,27,"bar"},
%%                {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%                 '$undefined'}}}}
%% (my_nam@127.0.0.1)462> L.
%% {{{function,{string,1,"main"},
%%             {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%                {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                        {pickup,8},
%%                        {flip,9}}},
%%               {branch,{funcall,{string,11,"foo"}},
%%                       {move,12},
%%                       {sense,{right_ahead,13},{food,13}}}},
%%              '$undefined'}},
%%   {function,{string,16,"foo"},
%%             {{{{move,18},{sense,{ahead,19},{home,19}}},
%%               {branch,{sense,{right_ahead,21},{foe_home,21}},
%%                       {pickup,22},
%%                       {flip,23}}},
%%              '$undefined'}}},
%%  {function,{string,27,"bar"},
%%            {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%             '$undefined'}}}
%% (my_nam@127.0.0.1)463> H.
%% ok
%% (my_nam@127.0.0.1)464> f().
%% ok
%% (my_nam@127.0.0.1)465> {ok, T} = c2a:c("sample.c").
%% {ok,{{{function,{string,1,"main"},
%%                 {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%                    {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                            {pickup,8},
%%                            {flip,9}}},
%%                   {branch,{funcall,{string,11,"foo"}},
%%                           {move,12},
%%                           {sense,{right_ahead,13},{food,13}}}},
%%                  '$undefined'}},
%%       {function,{string,16,"foo"},
%%                 {{{{move,18},{sense,{ahead,19},{home,19}}},
%%                   {branch,{sense,{right_ahead,21},{foe_home,21}},
%%                           {pickup,22},
%%                           {flip,23}}},
%%                  '$undefined'}}},
%%      {function,{string,27,"bar"},
%%                {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%                 '$undefined'}}}}
%% (my_nam@127.0.0.1)466> {H, L} = T.
%% {{{function,{string,1,"main"},
%%             {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%                {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                        {pickup,8},
%%                        {flip,9}}},
%%               {branch,{funcall,{string,11,"foo"}},
%%                       {move,12},
%%                       {sense,{right_ahead,13},{food,13}}}},
%%              '$undefined'}},
%%   {function,{string,16,"foo"},
%%             {{{{move,18},{sense,{ahead,19},{home,19}}},
%%               {branch,{sense,{right_ahead,21},{foe_home,21}},
%%                       {pickup,22},
%%                       {flip,23}}},
%%              '$undefined'}}},
%%  {function,{string,27,"bar"},
%%            {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%             '$undefined'}}}
%% (my_nam@127.0.0.1)467> L.
%% {function,{string,27,"bar"},
%%           {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%            '$undefined'}}
%% (my_nam@127.0.0.1)468> {_, _, B} = L.
%% {function,{string,27,"bar"},
%%           {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%            '$undefined'}}
%% (my_nam@127.0.0.1)469> B.
%% {{branch,{funcall,{string,29,"main"}},{pickup,30},{flip,31}},
%%  '$undefined'}
%% (my_nam@127.0.0.1)470> f().
%% ok
%% (my_nam@127.0.0.1)471> {ok, T} = c2a:c("sample.c").
%% {ok,{{{function,{string,1,"main"},
%%                 {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%                    {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                            {pickup,8},
%%                            {flip,9}}},
%%                   {branch,{funcall,{string,11,"foo"}},
%%                           {move,12},
%%                           {sense,{right_ahead,13},{food,13}}}},
%%                  '$undefined'}},
%%       {function,{string,16,"foo"},
%%                 {{{{move,18},{sense,{ahead,19},{home,19}}},
%%                   {branch,{sense,{right_ahead,21},{foe_home,21}},
%%                           {pickup,22},
%%                           {flip,23}}},
%%                  '$undefined'}}},
%%      {function,{string,27,"bar"},
%%                {{{{{move,29},{sense,{right_ahead,30},{foe,30}}},
%%                   {branch,{funcall,{string,31,"main"}},{pickup,32},{flip,33}}},
%%                  {flip,34}},
%%                 '$undefined'}}}}
%% (my_nam@127.0.0.1)472> {H, L} = T.
%% {{{function,{string,1,"main"},
%%             {{{{branch,{sense,{ahead,3},{food,3}},{pickup,4},{flip,5}},
%%                {branch,{sense,{left_ahead,7},{foe_with_food,7}},
%%                        {pickup,8},
%%                        {flip,9}}},
%%               {branch,{funcall,{string,11,"foo"}},
%%                       {move,12},
%%                       {sense,{right_ahead,13},{food,13}}}},
%%              '$undefined'}},
%%   {function,{string,16,"foo"},
%%             {{{{move,18},{sense,{ahead,19},{home,19}}},
%%               {branch,{sense,{right_ahead,21},{foe_home,21}},
%%                       {pickup,22},
%%                       {flip,23}}},
%%              '$undefined'}}},
%%  {function,{string,27,"bar"},
%%            {{{{{move,29},{sense,{right_ahead,30},{foe,30}}},
%%               {branch,{funcall,{string,31,"main"}},{pickup,32},{flip,33}}},
%%              {flip,34}},
%%             '$undefined'}}}
%% (my_nam@127.0.0.1)473> {_, _, B} = L.
%% {function,{string,27,"bar"},
%%           {{{{{move,29},{sense,{right_ahead,30},{foe,30}}},
%%              {branch,{funcall,{string,31,"main"}},{pickup,32},{flip,33}}},
%%             {flip,34}},
%%            '$undefined'}}
%% (my_nam@127.0.0.1)474> B.
%% {{{{{move,29},{sense,{right_ahead,30},{foe,30}}},
%%    {branch,{funcall,{string,31,"main"}},{pickup,32},{flip,33}}},
%%   {flip,34}},
%%  '$undefined'}
%% (my_nam@127.0.0.1)475> {HH, LL} = B.
%% {{{{{move,29},{sense,{right_ahead,30},{foe,30}}},
%%    {branch,{funcall,{string,31,"main"}},{pickup,32},{flip,33}}},
%%   {flip,34}},
%%  '$undefined'}
%% (my_nam@127.0.0.1)476> LL.
%% '$undefined'
%% (my_nam@127.0.0.1)477> HH.
%% {{{{move,29},{sense,{right_ahead,30},{foe,30}}},
%%   {branch,{funcall,{string,31,"main"}},{pickup,32},{flip,33}}},
%%  {flip,34}}
%% (my_nam@127.0.0.1)478> {HHH, LLL} = HH.
%% {{{{move,29},{sense,{right_ahead,30},{foe,30}}},
%%   {branch,{funcall,{string,31,"main"}},{pickup,32},{flip,33}}},
%%  {flip,34}}
%% (my_nam@127.0.0.1)479> LLL.
%% {flip,34}
%% (my_nam@127.0.0.1)480> 


